package com.fwj.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fwj.gmall.model.product.BaseTrademark;
import com.fwj.gmall.model.product.SpuImage;
import com.fwj.gmall.model.product.SpuInfo;
import com.fwj.gmall.model.product.SpuSaleAttr;
import com.fwj.gmall.model.product.SpuSaleAttrValue;
import com.fwj.gmall.product.mapper.BaseTrademarkMapper;
import com.fwj.gmall.product.mapper.SpuImageMapper;
import com.fwj.gmall.product.mapper.SpuInfoMapper;
import com.fwj.gmall.product.mapper.SpuSaleAttrMapper;
import com.fwj.gmall.product.mapper.SpuSaleAttrValueMapper;
import com.fwj.gmall.product.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpuServiceImpl implements SpuService {

    @Autowired
    SpuInfoMapper spuInfoMapper;

    @Autowired
    SpuSaleAttrMapper spuSaleAttrMapper;

    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    SpuImageMapper spuImageMapper;

    @Autowired
    SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Override
    public IPage<SpuInfo> spuList(Long page, Long limit, Long category3Id) {
        Page<SpuInfo> infoPage = new Page<>();
        infoPage.setSize(limit);
        infoPage.setCurrent(page);

        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("category3_id", category3Id);

        IPage<SpuInfo> iPage = spuInfoMapper.selectPage(infoPage, wrapper);
        return iPage;
    }

    @Override
    public List<SpuSaleAttr> baseSaleAttrList() {
        return spuSaleAttrMapper.selectList(null);
    }

    @Override
    public List<BaseTrademark> getTrademarkList() {
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void saveSpuInfo(SpuInfo spuInfo) {
        spuInfoMapper.insert(spuInfo);
        // 获取插入后的主键
        Long spuId = spuInfo.getId();
        // 插入spuImage
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (spuImageList != null && spuImageList.size() > 0) {
            for (SpuImage spuImage : spuImageList) {
                spuImage.setSpuId(spuId);
                spuImageMapper.insert(spuImage);
            }
        }
        // 插入spuSaleAttr
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (spuSaleAttrList != null && spuImageList.size() > 0) {
            for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
                spuSaleAttr.setSpuId(spuId);
                spuSaleAttrMapper.insert(spuSaleAttr);
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (spuSaleAttrValueList != null && spuSaleAttrValueList.size() > 0) {
                    for (SpuSaleAttrValue spuSaleAttrValue : spuSaleAttrValueList) {
                        spuSaleAttrValue.setSpuId(spuId);
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                        spuSaleAttrValueMapper.insert(spuSaleAttrValue);
                    }
                }
            }
        }
    }

    @Override
    public List<SpuImage> spuImageList(Long spuId) {

        return spuImageMapper.selectList(null);

    }

    @Override
    public List<SpuSaleAttr> spuSaleAttrList(Long spuId) {
        QueryWrapper<SpuSaleAttr> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_id", spuId);

        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrMapper.selectList(wrapper);

        for (SpuSaleAttr spuSaleAttr : spuSaleAttrList) {
            Long id = spuSaleAttr.getSpuId();
            QueryWrapper<SpuSaleAttrValue> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("spu_id", id);
            List<SpuSaleAttrValue> spuSaleAttrValues = spuSaleAttrValueMapper.selectList(queryWrapper);
            spuSaleAttr.setSpuSaleAttrValueList(spuSaleAttrValues);
        }

        return spuSaleAttrList;
    }
}
